package com.qf.zsq.controller;

import com.qf.entity.GoodsEntity;
import com.qf.entity.OrderEntity;
import com.qf.entity.ResponseResult;
import com.qf.pojo.AddressPojo;
import com.qf.pojo.CustomerPojo;
import com.qf.pojo.OrderPojo;
import com.qf.zsq.service.AddressService;
import com.qf.zsq.service.IOrderService;
import com.qf.zsq.task.OrderTask;
import com.qf.zsq.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;
import java.util.Timer;

@Controller
public class OrderController {
    @Autowired
    IOrderService orderService;
    @Autowired
    AddressService addressService;

    @RequestMapping("jiesuan")
    public String jiesuan(String[] goodscheck, Map<String, GoodsEntity> shopCar, HttpServletRequest request, String type) {

        shopCar = (Map<String, GoodsEntity>) request.getSession().getAttribute("shopCar");
        if (type.equals("now")) {
            goodscheck = shopCar.keySet().toArray(new String[shopCar.keySet().size()]);
        }
        if (null == goodscheck) {
            return "shopcart";
        }
        CustomerPojo customer = (CustomerPojo) request.getSession().getAttribute("customer");

        Integer cid = customer.getCid();//获取用户id填入订单表

        ResponseResult rrmsg = (ResponseResult) request.getSession().getAttribute("msg");
        String[] split = rrmsg.getResultMsg().split(",");
        Float odPrice = Float.parseFloat(split[1]);//购物车订单目前总价格

        OrderEntity orderEntity = new OrderEntity();
        orderEntity.setCid(cid);
        orderEntity.setOid(StringUtil.getUUID().toString().replace("-", ""));
        orderEntity.setOtotalPrice(odPrice);

        OrderEntity order = orderService.createOrder(orderEntity, shopCar, goodscheck);
        request.getSession().removeAttribute("shopCar");
        request.getSession().removeAttribute("msg");
        List<AddressPojo> addressPojos = addressService.queryAddress(customer.getCid());
        request.setAttribute("alist",addressPojos);
        request.setAttribute("order",order);
        return "accounts";
    }

    @RequestMapping("jumpPay")
    public String jumpPay(HttpServletRequest request, OrderEntity oe) {
        boolean b = orderService.addAddressToOrder(oe.getAddressId(), oe.getOid());

        Timer timer = new Timer();
        timer.schedule(new OrderTask(orderService, oe.getOid(), timer), 1800 * 1000);
        request.getServletContext().setAttribute(oe.getOid(),timer);
        request.getSession().removeAttribute("allMoney");
        return "redirect:selectForPay?oid="+oe.getOid();
    }

    @RequestMapping("selectForPay")
    public String selectForPay(String oid,HttpServletRequest request){
        OrderPojo orderPojo = orderService.selectOrder(oid);
        request.setAttribute("order",orderPojo);
        request.setAttribute("oid",oid);
        return "orderinfo";
    }

    @RequestMapping("pay")
    public String pay(HttpServletRequest request,String oid){
        Timer timer = (Timer) request.getServletContext().getAttribute(oid);
        timer.cancel();
        boolean b = orderService.payMon(oid);
        if(b) {
            request.getServletContext().removeAttribute(oid);
            return "success";
        }
        return "fail";
    }

    @RequestMapping("queryOrderList")
    public String queryOrderList(Integer cid,HttpServletRequest request){
        List<OrderPojo> orderPojos = orderService.selectOrderById(cid);
        request.setAttribute("orderList",orderPojos);
        return "orderlist";
    }
}

